未來社會中,文盲並非不識字的人,而是不能再學習的人。鐵人賽就是強迫自己學習的好機會。
大家好今天我要來介紹如何使用FFmpeg來切割影片,他有很多其他的功能但我今天只會來介紹切影片的功能,大家如果有想要嘗試其他功能的話可以去網路上查詢。廢話不多說那我們就開始示範吧!
一開始先在gradle加入:
implementation 'com.arthenica:mobile-ffmpeg-full:4.3.2'
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/mButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點擊我"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
當使用者按下按鈕時,app會跳到選取影片的畫面,在點選影片後就會開始分割影片了。
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.arthenica.mobileffmpeg.Config;
import com.arthenica.mobileffmpeg.FFmpeg;
import com.arthenica.mobileffmpeg.FFprobe;
import java.io.File;
import java.util.ArrayList;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_CANCEL;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;
public class MainActivity extends AppCompatActivity {
Button mButton;
private static final int PICK_VIDEO_FROM_GALLERY_REQUEST_CODE = 400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickImageFromGallery();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_VIDEO_FROM_GALLERY_REQUEST_CODE && resultCode == RESULT_OK) {
} if (Build.VERSION.SDK_INT >= 16 && data.getClipData() == null) {
ArrayList<String> uriList = new ArrayList<String>();
uriList.clear();
String path = FileUtil.getFileAbsolutePath(this, data.getData());
String name = FileUtil.fileName(path);
uriList.add(path);
ArrayList<String> VideoList = new ArrayList<>();
VideoList.add(path);
Log.e("Listsize", VideoList.size() + ""+path);
Log.e("List", VideoList.get(0));
segVideo(path);
}
}
private void pickImageFromGallery() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
String[] mimeTypes = {"video/mp4", "video/mov"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, PICK_VIDEO_FROM_GALLERY_REQUEST_CODE);
}
public void segVideo(String path){
ArrayList<String> segVideoList = new ArrayList<>();
File video = new File(path);
int videoSize = (int) video.length();
int sizeLimit = 50 * 1024 * 1024;
Log.e("size", video.length() + "");
int duration = (int) (FFprobe.getMediaInformation(path).getDuration() / 1000); //影片秒數
int startTime = 0;
int segTime = 0; //分割秒數
int loop = 0;
int sss = videoSize / sizeLimit;
int aaa = videoSize % sizeLimit;
if (videoSize > sizeLimit) { //如果video大小超過設定的sizelimit大小
//計算要分割的秒數
if (aaa != 0) {
segTime = duration / (sss + 1);
}
//迴圈次數
if (duration % segTime != 0) {
loop = duration / segTime + 1;
} else {
loop = duration / segTime;
}
//開始分割
for (int i = 0; i < loop; i++) {
int rc = FFmpeg.execute("-ss " + startTime + " -i " + path + " -t " + segTime + " -acodec copy -vcodec copy " + Environment.getExternalStorageDirectory().toString() + "/Seg_Video" + "/" + video.getName().replace(".mp4", "") + "_seg_" + i + ".mp4");
if (rc == RETURN_CODE_SUCCESS) {
segVideoList.add(Environment.getExternalStorageDirectory().toString() + "/Seg_Video" + "/" + video.getName().replace(".mp4", "") + "_seg_" + i + ".mp4");
Log.i(Config.TAG, "Command execution completed successfully.");
} else if (rc == RETURN_CODE_CANCEL) {
Log.i(Config.TAG, "Command execution cancelled by user.");
} else {
Log.i(Config.TAG, String.format("Command execution failed with rc=%d and the output below.", rc));
Config.printLastCommandOutput(Log.INFO);
}
startTime += segTime;
}
} else {
//沒超過
segVideoList.add(path);
}
}
}
我來解釋一下在MainActivity的的程式碼裡面最重要的有影片切割功能的是下面的程式碼:
int rc = FFmpeg.execute("-ss " + startTime + " -i " + path + " -t " + segTime + " -acodec copy -vcodec copy " + Environment.getExternalStorageDirectory().toString() + "/Seg_Video" + "/" + video.getName().replace(".mp4", "") + "_seg_" + i + ".mp4");
在這段程式碼裡面
AndroidManifest 裡面加入
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
喔對了如果各位的Android系統是10以上的話記得在AndroidManifest的application裡面加入:
android:requestLegacyExternalStorage="true"
否則Logcat會報錯
那今天的介紹就到這了,謝謝大家的觀看。